const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

// 定义 Fruit 模型
const Fruit = sequelize.define('Fruit', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '水果ID'
  },
  name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    validate: {
      notNull: {
        msg: '水果名称不能为空'
      },
      notEmpty: {
        msg: '水果名称不能为空'
      }
    },
    comment: '水果名称'
  },
  category: {
    type: DataTypes.STRING(50),
    allowNull: false,
    validate: {
      notNull: {
        msg: '分类不能为空'
      },
      notEmpty: {
        msg: '分类不能为空'
      }
    },
    comment: '水果分类'
  },
  price: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    validate: {
      notNull: {
        msg: '价格不能为空'
      }
    },
    comment: '价格'
  },
  total_stock: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    validate: {
      notNull: {
        msg: '总库存不能为空'
      },
      min: {
        args: [0],
        msg: '总库存不能为负数'
      }
    },
    comment: '总库存数量（采购总量）'
  },
  stock: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    validate: {
      notNull: {
        msg: '可用库存不能为空'
      },
      min: {
        args: [0],
        msg: '可用库存不能为负数'
      }
    },
    comment: '可用库存数量（未分配到仓库的数量）'
  },
}, {
  tableName: 'fruits',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

module.exports = Fruit;

